package com.hkts.reportform.datastatics.controller;

import com.google.gson.Gson;
import com.hkts.reportform.datastatics.entity.ReportForm;
import com.hkts.reportform.datastatics.service.ReportService;
import com.hkts.reportform.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassNamereportController
 * @Description erp大报表数据展示页面
 * @Author yck
 * @Date2020-10-19 11:13
 * @Version V1.0  /bigScreen
 **/
@RestController
@RequestMapping("/erp")
public class ReportController {
    @Autowired
    private ReportService reportServiceImpl;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/selectInventory")
    public List<Map<String, Object>> selectInventory(){
        List<Map<String, Object>> maps = reportServiceImpl.selectInventory();
        return maps;
    }
    //沥青消耗图
    //沥青分析图
    @RequestMapping("/asphaltData")
    public Result<Object> asphaltData(){
        Map<String,Object> map=new HashMap<>();//ASPHALT_CONSUME
        map.put("asphaltConsume",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.ASPHALT_CONSUME,0,-1)));
        map.put("asphaltStock",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.ASPHALT_STOCK,0,-1)));
        map.put("dateArray", DateUtil.getShowDateArray(7));
        return ResultUtil.data(map);
    }

    //熟块名称列表
    @RequestMapping("/cookeBilletNames")
    public  Result<Object> cookeBilletNames() throws Exception{
        return ResultUtil.data(GsonUtil.conversionToReportForm(redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_BUTTONS).toString()));
    }
    //熟坯7日内曲线
    @RequestMapping("/cookeBilletCurve")
    public  Result<Object> cookeBilletCurve(String stockCode) throws Exception{
        Map<String,Object> map=new HashMap<>();
        map.put("dateArray",DateUtil.getDateArray(7));
        map.put("valueArray", SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.COOKED_BILLET_WEEK_CODE+stockCode,0,-1)));
        return ResultUtil.data(map);
    }
    //圆饼比例 cookedBillet
    @RequestMapping("/cookeBilletRate")
    public  Result<Object> cookeBilletRate() throws Exception{
        Map<String,Object> map=new HashMap<>();
        map.put("nameArray",redisTemplate.opsForList().range(ErpProperties.COOKES_BILLET_NAME,0,-1));
        map.put("valueArray",redisTemplate.opsForList().range(ErpProperties.COOKES_BILLET_VALUE,0,-1) );
        return ResultUtil.data(map);
    }
    //辅材消耗分析
    @RequestMapping("/categoryOfauxiliaryMaterials")
    public Result<Object> categoryOfauxiliaryMaterials(String dateType){
        Map<String,Object> map=new HashMap<>();
        List<String> list=new ArrayList<String>();
        list.add("机械");
        list.add("电器");
        list.add("材料");
        list.add("办公");
        list.add("基建");
        list.add("其他");
        map.put("typeArray",list);
        if (dateType.equals("0")){
            map.put("valueArray",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.AUXILIARY_MATERIALS_DAY,0,-1)) );
            map.put("sortArray",GsonUtil.conversionToReportForm(redisTemplate.opsForValue().get(ErpProperties.AUXILIARY_MATERIALS_SORT_DAY).toString()));
        }else if(dateType.equals("1")){
            map.put("valueArray",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.AUXILIARY_MATERIALS_WEEK,0,-1)) );
            map.put("sortArray",GsonUtil.conversionToReportForm(redisTemplate.opsForValue().get(ErpProperties.AUXILIARY_MATERIALS_SORT_WEEK).toString()));
        }else {
            map.put("valueArray",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.AUXILIARY_MATERIALS_MONTH,0,-1)) );
            map.put("sortArray",GsonUtil.conversionToReportForm(redisTemplate.opsForValue().get(ErpProperties.AUXILIARY_MATERIALS_SORT_MONTH).toString()));
        }
        return ResultUtil.data(map);
    }
    //石油焦,生坯,熟坯
    @RequestMapping("/petroleumCokeData")
    public Result<Object>  petroleumCokeData(){
        Map<String,Object> map=new HashMap<>();
        map.put(ErpProperties.PETROLEUM_COKE_WEEK,redisTemplate.opsForValue().get(ErpProperties.PETROLEUM_COKE_WEEK));
        map.put(ErpProperties.GREEN_BODY_WEEK,redisTemplate.opsForValue().get(ErpProperties.GREEN_BODY_WEEK));
        map.put(ErpProperties.COOKED_BILLET_WEEK,redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_WEEK));

        map.put(ErpProperties.PETROLEUM_COKE_MONTH,redisTemplate.opsForValue().get(ErpProperties.PETROLEUM_COKE_MONTH));
        map.put(ErpProperties.GREEN_BODY_MONTH,redisTemplate.opsForValue().get(ErpProperties.GREEN_BODY_MONTH));
        map.put(ErpProperties.COOKED_BILLET_MONTH,redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_MONTH));
        return ResultUtil.data(map);
    }
    //煅后焦消耗曲线
    @RequestMapping("/consumptionAnalysisOfCalcinedCoke")
    public Result<Object>  consumptionAnalysisOfCalcinedCoke(){
        Map<String,Object> map=new HashMap<>();
        map.put("dateArray",DateUtil.getDateArray(7));
        map.put("valueArray",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.CALCINED_COKE_WEEK_CONSUME,0,-1)) );
        return ResultUtil.data(map);
    }
    //雷达图radarChartOfpetroleumCoke
    @RequestMapping("/radarChartOfpetroleumCoke")
    public Result<Object>  radarChartOfpetroleumCoke(){
        List list = redisTemplate.opsForList().range(ErpProperties.PETROLEUM_COKE_RATE_NAMES, 0, -1);
        List list1 = redisTemplate.opsForList().range(ErpProperties.PETROLEUM_COKE_RATE_VALUES, 0, -1);
        String[] colorArray={"#00c7ff", "#f54d4d", "#f87544", "#ffae00", "#dcff00", "#25d053", "#4245ff", "#c32eff", "#00FF00",
                "#FAFAD2", "#AFEEEE", "#FF4500", "#CD5C5C"};
        List<Map<String,Object>> result=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Map<String,Object> map=new HashMap<>();
            map.put("name",list.get(i).toString().substring(0,list.get(i).toString().length()-4));
            map.put("value",list1.get(i));
            map.put("color",colorArray[i/colorArray.length]);
            result.add(map);
        }
        return ResultUtil.data(result);
    }
    @RequestMapping("/totalTable")
    public Result<Object>  totalTable(){
        Map<String,Object> result=new HashMap<>();
     //沥青分析消耗图
        Map<String,Object> liqinfenxitu=new HashMap<>();
        liqinfenxitu.put("asphaltConsume",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.ASPHALT_CONSUME,0,-1)));
        liqinfenxitu.put("asphaltStock",SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.ASPHALT_STOCK,0,-1)));
        liqinfenxitu.put("dateArray", DateUtil.getShowDateArray(7));
        result.put("liQinFenXiTu",liqinfenxitu);
        //熟块名称列表
        Map<String,Object> shuKuaiResult=new HashMap<>();
        List<Map<String,Object>> shuKuaiValueArray=new ArrayList<>();
        List<ReportForm> reportForms = GsonUtil.conversionToReportForm(redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_BUTTONS).toString());
        List<String> listCodes=  redisTemplate.opsForList().range(ErpProperties.COOKED_BILLET_CODES,0,-1);
        Map<String,Object> shuKuaiValue=new HashMap<>();
        for(int i=0;i<listCodes.size();i++){
            shuKuaiValue.put(listCodes.get(i),SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.COOKED_BILLET_WEEK_CODE+listCodes.get(i),0,-1)));
        }
        shuKuaiResult.put("shuKuaiLieBiao",reportForms);
        shuKuaiResult.put("shuKuaiValueArray",shuKuaiValue);
        shuKuaiResult.put("shuKuaiDateArray",DateUtil.getShowDateArray(7));
        result.put("shukuai",shuKuaiResult);
        //圆饼比例 熟块库存比例
        Map<String,Object>shukuaikucunbili=new HashMap<>();
        shukuaikucunbili.put("nameArray",redisTemplate.opsForList().range(ErpProperties.COOKES_BILLET_NAME,0,-1));
        shukuaikucunbili.put("valueArray",redisTemplate.opsForList().range(ErpProperties.COOKES_BILLET_VALUE,0,-1) );
        result.put("shuKuaiKuCunBiLi",shukuaikucunbili);
        //雷达图
        List list = redisTemplate.opsForList().range(ErpProperties.PETROLEUM_COKE_RATE_NAMES, 0, -1);
        List list1 = redisTemplate.opsForList().range(ErpProperties.PETROLEUM_COKE_RATE_VALUES, 0, -1);
        String[] colorArray={"#00c7ff", "#f54d4d", "#f87544", "#ffae00", "#dcff00", "#25d053", "#4245ff", "#c32eff", "#00FF00",
                "#FAFAD2", "#AFEEEE", "#FF4500", "#CD5C5C"};
        List<Map<String,Object>> leidatu=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Map<String,Object> map=new HashMap<>();
            String s = list.get(i).toString().replace("石油焦", "");
            map.put("name",s);
            map.put("value",list1.get(i));
            map.put("color",colorArray[i%colorArray.length]);
            leidatu.add(map);
        }
        result.put("leiDaTu",leidatu);
        //煅后焦消耗曲线
        Map<String,Object> duanHouJiaoXiaoHaoQuXian=new HashMap<>();
        duanHouJiaoXiaoHaoQuXian.put("dateArray",DateUtil.getShowDateArray(7));
        duanHouJiaoXiaoHaoQuXian.put("valueArray", SortUtils.reverstList(redisTemplate.opsForList().range(ErpProperties.CALCINED_COKE_WEEK_CONSUME,0,-1)) );
        result.put("duanHouJiaoXiaoHaoQuXian",duanHouJiaoXiaoHaoQuXian);
        //shiYouShenPiShuPi
        Map<String,Object> shiYouShenPiShuPi=new HashMap<>();
        shiYouShenPiShuPi.put(ErpProperties.PETROLEUM_COKE_WEEK,redisTemplate.opsForValue().get(ErpProperties.PETROLEUM_COKE_WEEK));
        shiYouShenPiShuPi.put(ErpProperties.GREEN_BODY_WEEK,redisTemplate.opsForValue().get(ErpProperties.GREEN_BODY_WEEK));
        shiYouShenPiShuPi.put(ErpProperties.COOKED_BILLET_WEEK,redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_WEEK));

        shiYouShenPiShuPi.put(ErpProperties.PETROLEUM_COKE_MONTH,redisTemplate.opsForValue().get(ErpProperties.PETROLEUM_COKE_MONTH));
        shiYouShenPiShuPi.put(ErpProperties.GREEN_BODY_MONTH,redisTemplate.opsForValue().get(ErpProperties.GREEN_BODY_MONTH));
        shiYouShenPiShuPi.put(ErpProperties.COOKED_BILLET_MONTH,redisTemplate.opsForValue().get(ErpProperties.COOKED_BILLET_MONTH));
        result.put("shiYouShenPiShuPi",shiYouShenPiShuPi);
        return ResultUtil.data(result);
    }
}
